#include <stdio.h>
#include "mpi.h"

#define COUNT 5

int main(int argc, char **argv)
{
    MPI_Datatype newtype;
    MPI_Status status;
    int count;
    int failure = 0;
    
    MPI_Init(&argc, &argv);

    MPI_Type_contiguous(COUNT, MPI_INT, &newtype);

    /* cheat the status */
    status.bytes_recvd = 20;

    MPI_Get_count(&status, MPI_INT, &count);
    fprintf(stdout, "should equal 5: %d\n", count);
    if (count != 5) failure = 1;
    
    MPI_Get_elements(&status, MPI_INT, &count);
    fprintf(stdout, "should equal 5: %d\n", count);
    if (count != 5) failure = 1;

    MPI_Get_count(&status, newtype, &count);
    fprintf(stdout, "should equal 1: %d\n", count);
    if (count != 1) failure = 1;
    
    MPI_Get_elements(&status, newtype, &count);
    fprintf(stdout, "should equal 5: %d\n", count);
    if (count != 5) failure = 1;

    status.bytes_recvd = 28;
    
    MPI_Get_elements(&status, newtype, &count);
    fprintf(stdout, "should equal 7: %d\n", count);
    if (count != 7) failure = 1;

    MPI_Get_count(&status, newtype, &count);
    
    if (count == MPI_UNDEFINED)
        fprintf(stdout, "Count undefined (yes)? yes\n");
    else
    {
        fprintf(stdout, "Count undefined (yes)? no\n");
        if (count != 5) failure = 1;
    }
     
    if (failure)
        fprintf(stderr, "FAILURE\n");
    else
        fprintf(stderr, "SUCCESS\n");

    MPI_Finalize();
    
    return(0);
}

